/*********************************************
 * EIRMULATOR is under the beerware license.
 *
 * THE BEERWARE LICENSE (Revision 42):
 *
 * As long as you retain this notice you can
 * do whatever you want with this stuff. If we
 * meet some day, and you think this stuff is 
 * worth it, you can buy me a drink in return.
 */


/** \file loutre.h
 * \brief Definit CLoutre la classe gerant l'affichage et l'user i/o.
 * \author JDaM <brossill@enseirb.fr>
 * \version 0.8.0
 * \date 28.12.2005 @ 20:02
 *
 */

#ifndef LOUTRE_H
#define LOUTRE_H

#include <stdlib.h>

#include <SDL/SDL.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>


#include "robot.h"
#include "object.h"
#include "sensor.h"

#include "compteur.h"
#include "configuration.h"

#include <list>

using namespace std;

extern CConfiguration configuration;

class CLoutre
{

public:
  
  typedef struct _CAMPOS
  {
    float angle;
    float avance;
    float hauteur;
    
    _CAMPOS() { angle=0.0f;  avance=0.0f; hauteur=0.0f;};
    
  }CAMPOS;
  
  /* Trucs */

  bool start_thread;

  list<CObject*> objects;
  list<CSensor*> sensors;

private:

  /* Membres */

  GLfloat no_mat[4];
  GLfloat mat_ambient[4];
  GLfloat mat_ambient_color[4];
  GLfloat mat_diffuse[4];
  GLfloat mat_specular[4];
  GLfloat no_shininess[1];
  GLfloat low_shininess[1];
  GLfloat high_shininess[1];
  GLfloat mat_emission[4];

  /* Gameplay on va dire... */
  
  CCompteur compteur;
  
  /* Graphismes */
  bool pause;
  
  CAMPOS camera;
  
  int screen_X;
  int screen_Y;

  int sdl_bpp;
  int sdl_flags;

  SDL_Surface* screen;

  int fpsN;

  double fps;
  
  int dt_last_frame;
 
/* NDJD : Les fonctions drawString & drawStringBig on étés
   récupérés dans les exemples fournis par Miguel A Sepulveda.

   http://www.linuxfocus.org/English/March1998/article29.html
*/ 

  void drawString(char*);

  void drawStringBig(char*);
  
  /* robots */

  CRobot* robots[MAX_ROBOTS];

  
  double cible_x;
  double cible_y;
	
public:

  char robot_script_1[128],robot_script_2[128];

/* Constructeur/Destructeur */
public:
  
  CLoutre();
  ~CLoutre();

  /* Methodes */
  
private:
  
  void handle_events();

  void handle_keydown(SDL_keysym* keysym);

  void handle_keys();


  void window_init(int,int,bool);

  void window_close();

  void window_resize(int,int);

  
public:

  /* Placements */
  
  void placer_robots(dWorldID, dSpaceID);

  /* Declaration */

  void declare_robots(CRobot*, CRobot*);

  /* Scene 3D */
  
  void scene_init();

  void scene_deinit();

  void scene_debut();

  void scene_fin();

  /* Nettoyeur */
  
public:

  /* Accesseurs */

  int get_screen_x() { return this->screen_X;};
  int get_screen_y() { return this->screen_Y;};

  void set_screen_x(int x) { this->screen_X=x;};
  void set_screen_y(int y) { this->screen_Y=y;};

  void set_camera(float ag,float av,float ha)
  {
    this->camera.angle = ag;
    this->camera.avance = av;
    this->camera.hauteur = ha;
  };
  
  /* Fonctions utilisateur */

  /** \todo Trouver un commentaire sympa... */

  void init(int,int,bool);

  void deinit();
  
  void update(CRobot**);

  /* */

  void set_cible(double x,double y) { this->cible_x = x; this->cible_y = y; };
  void afficher_cible();

  void afficher_message(double,double,char*);
	
  /* Affichage des objets */

  void afficher_table();

  void afficher_robot(CRobot*);

  void afficher_objet(CObject*);

  void afficher_capteur(CSensor*);

  /* Affiche un pave en rotation descentre */
  void afficher_paveRD(dReal x, dReal y, dReal z,
		       dReal ux, dReal uy, dReal uz, dReal alpha,
		       dReal tx, dReal ty, dReal tz,
		       float r, float g, float b);
	
  void afficher_cylindre(dReal x, dReal y, dReal z,
			 dReal ux, dReal uy, dReal uz, dReal alpha,
			 dReal tx, dReal ty, dReal tz,
			 float r, float g, float b);

};

#endif /* LOUTRE_H */
